# coding=utf-8
import os.path


def insert_line(p_path, idx, msg):
    if os.path.exists(p_path):
        with open(p_path, 'r+') as fr:
            lines = fr.readlines()
            fr.seek(0, 0)
            lines.insert(idx, f'{msg}\n')
            fr.writelines(lines)
    else:
        with open(p_path, 'w') as fw:
            fw.write(msg)
            fw.write('\n')
